package com.example.cardgame

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import org.w3c.dom.Text
import java.io.ByteArrayOutputStream

class Alter : ComponentActivity() {

    private lateinit var radioGroup: RadioGroup
    private var imageBitmap: Bitmap? = null

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

            imageBitmap = data?.extras?.get("data") as Bitmap
            val imageView = findViewById<ImageView>(R.id.alter_image)
            imageView.setImageBitmap(imageBitmap)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.alter)
        val cancel=findViewById<Button>(R.id.cancel_button)
        cancel.setOnClickListener{
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)

        }
        val button = findViewById<Button>(R.id.pick)
        button.setOnClickListener { dispatchTakePictureIntent() }
        radioGroup = findViewById(R.id.group)
        radioGroup.setOnCheckedChangeListener { _, checkedId ->
            val selectedSex = when(checkedId) {
                R.id.choice_man -> "男"
                R.id.choice_woman -> "女"
                else -> "未知"
            }
            Log.d("SelectedSex", "Selected Sex: $selectedSex")
        }

        val confirm = findViewById<Button>(R.id.save_button)
        confirm.setOnClickListener {
            val imageBytes: ByteArray = ByteArrayOutputStream().use { stream ->
                imageBitmap?.compress(Bitmap.CompressFormat.PNG, 100, stream)
                stream.toByteArray()
            }
            val intent = Intent().apply {

                putExtra(NAME_MSG, findViewById<EditText>(R.id.alter_name).text.toString())
                putExtra(NUMBER_MSG, findViewById<EditText>(R.id.alter_number).text.toString())
                putExtra(PHONE_MSG, findViewById<EditText>(R.id.alter_phone).text.toString())
                putExtra(EMAIL_MSG, findViewById<EditText>(R.id.alter_email).text.toString())
                putExtra(SEX_MSG, radioGroup.checkedRadioButtonId.let {
                    when(it) {
                        R.id.choice_man -> "男"
                        R.id.choice_woman -> "女"
                        else -> "未知"
                    }
                })
                putExtra(HOBBY_MSG, getSelectedHobbies())
                putExtra(IMAGE_MSG,imageBytes)
            }
            setResult(RESULT_OK, intent)
            finish()
        }



    }
    val REQUEST_IMAGE_CAPTURE = 1

    private fun dispatchTakePictureIntent() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (intent.resolveActivity(packageManager) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
        }
    }

    private fun getSelectedHobbies(): String {
        val hobbies = mutableListOf<String>()
        listOf(
            findViewById<CheckBox>(R.id.c_hobby1),
            findViewById<CheckBox>(R.id.c_hobby2),
            findViewById<CheckBox>(R.id.c_hobby3),
            findViewById<CheckBox>(R.id.c_hobby4)
        ).forEach {
            if (it.isChecked) {
                hobbies.add(it.text.toString())
            }
        }
        return hobbies.joinToString(", ")
    }
}